<?php
	class Controller_Email extends Controller_Abstract{
		
		function actionIndex(){
			if(isset($_SESSION['errors'])){
				$this->_view['errors'] = $_SESSION['errors'];
				unset($_SESSION['errors']);
			}
			if(isset($_SESSION['infos'])){
				$this->_view['infos'] = $_SESSION['infos'];
				unset($_SESSION['infos']);
			}
			$email_model = new Email();
			$this->_view['templates'] = $email_model->getTemplates();
		}
		
		function actionAdd(){
			if(isset($_SESSION['errors'])){
				$this->_view['errors'] = $_SESSION['errors'];
				unset($_SESSION['errors']);
			}
			if($this->_context->isPOST()){
				$tag = $this->_context->tag;
				$subject = $this->_context->subject;
				$sender = $this->_context->sender;
				$content = $this->_context->content;
				$email_model = new Email();
				if($email_model->addTemplate($tag, $subject, $sender, $content)){
					$infos = array();
					$infos[] = Util::getMessage(10003);
					$_SESSION['infos'] =$infos;
					return $this->_redirect(url('email/index'));
				} else {
					$errors = array();
					$errors[] = Util::getMessage(1019);
					$_SESSION['errors'] = $errors;
				}
			}
		}
		
		function actionUpdate(){
			$id = $this->_context->id;
			$errors = array();
			if(empty($id)){
				$errors[] = Util::getMessage(1017);
				$_SESSION['errors'] = $errors;
				return $this->_redirect(url('email/index'));
			}
			if(isset($_SESSION['errors'])){
				$this->_view['errors'] = $_SESSION['errors'];
				unset($_SESSION['errors']);
			}
			if(isset($_SESSION['infos'])){
				$this->_view['infos'] = $_SESSION['infos'];
				unset($_SESSION['infos']);
			}
			$email_model = new Email();
			if($this->_context->isGET()){
				$template = $email_model->getTemplate($id);
				$this->_view['id'] = $template['id'];
				$this->_view['tag'] = $template['tag'];
				$this->_view['subject'] = $template['subject'];
				$this->_view['sender'] = $template['sender'];
				$this->_view['content'] = $template['content'];
			} elseif ($this->_context->isPOST()){
				$tag = $this->_context->tag;
				$subject = $this->_context->subject;
				$sender = $this->_context->sender;
				$content = $this->_context->content;
				if($email_model->editTemplate($tag, $subject, $sender, $content, $id)){
					$infos = array();
					$infos[] = Util::getMessage(10001);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('email/index'));
				} else {
					$errors[] = Util::getMessage(1019);
					$_SESSION['errors'] = $errors;
				}
				$this->_view['tag'] = $tag;
				$this->_view['subject'] = $subject;
				$this->_view['sender'] = $sender;
				$this->_view['content'] = $content;
				$this->_view['id'] = $id;
			}
		}
		
		function actionDelete(){
			$id = $this->_context->id;
			$errors = array();
			if(empty($id)){
				$errors[] = Util::getMessage(1017);
				$_SESSION['errors'] = $errors;
				return $this->_redirect(url('email/index'));
			}
			$email_model = new Email();
			if($email_model->deleteTemplate($id)){
				$infos = array();
				$infos[] = Util::getMessage(10002);
				$_SESSION['infos'] = $infos;
				return $this->_redirect(url('email/index'));
			} else {
				$errors[] = Util::getMessage(1019);
				$_SESSION['errors'] = $errors;
				return $this->_redirect(url('email/index'));
			}
		}
		
		function actionPreview(){
			$id = $this->_context->id;
			$errors = array();
			if(empty($id)){
				$errors[] = Util::getMessage(1017);
				$_SESSION['errors'] = $errors;
				return $this->_redirect(url('email/index'));
			}
			$email_model = new Email();
			$template = $email_model->getTemplate($id);
			$this->_view['tag'] = $template['tag'];
			$this->_view['subject'] = $template['subject'];
			$this->_view['sender'] = $template['sender'];
			$this->_view['content'] = $template['content'];
		}
		
		function actionTestsend(){
			if($this->_context->isPOST()){
				if($isgettpl = $this->_context->post('gettpl', false)){
					$site = $this->_context->post('site', '');
					$tpls = Util::getSiteTpls($site);
					return json_encode ( $tpls );
				}else{
					$tpl = $this->_context->post('emailtpl', '');
					$sendto = $this->_context->post('sendto', '');
					$site = $this->_context->post('sites', '');
					
					if(Util::addNewEmail($tpl, $site, array('email'=>$sendto, 'domain'=>$site, 'priority'=>10000))){
						return '1';
					}
					return '0';				
				}
			}else{
				$site = 'cashdazzle.com';
				$tpls = Util::getSiteTpls($site);
				
				$sitesArray = Array('cashdazzle.com', 'lalaloot.com', 'mommymoolah.com', 'cashdazzle.co.uk', 'lalaloot.co.uk', 'mommymoolah.co.uk');
				$this->_view['sitesArray'] = $sitesArray;
				$this->_view['site'] = $site;
				$this->_view['tpls'] = $tpls;
			}
		}
		
		function actionTestoffersofweek(){
			$emailOffersModel = new EmailOffers();
			if($this->_context->isPOST()){
				$userEmail = $this->_context->post('userEmail', '');
				if($userEmail != ''){
					$tag = 'offersofweek';
					
					$siteModel = new Site();
					$sitesInfo = $siteModel->getAllSites();
					
					foreach ($sitesInfo as $site) {
						$userModel = new User();
//						$user = $userModel->getUserByField('email', $userEmail);
//						if(empty($user) || $user['site_id'] != $site['id']){
//							continue;
//						}

						$emailOffers = $emailOffersModel->getAllEmailOffersBySite($site['id']);
						if(!empty($emailOffers)){
							$emailOffersBySentdate = array();
							foreach($emailOffers as $k=>$v){
								$emailOffersBySentdate[$v['tosent_date']][] = $v;
							}
						
							foreach($emailOffersBySentdate as $toSentdate=>$emailOffersEachDate){
								$templateModel = new Template();
								$template = $templateModel->getTemplate($tag, $site['template']);
								//$subject = Util::getEmailSubject($tag, $site['template'], array(), 'db', $site['id']);
								$subject = $emailOffersEachDate[0]['subject'];
									
								$args = array();
								$args['firstname'] = 'Test';
								$testUid = 0;
								
								$translatedSubject = Util::translate($subject, $args);
								
								$emailModel = new Email();
								$emailId = $emailModel->addSentEmail($site['name'], 'no-reply@' . strtolower($site['domain']), $translatedSubject, $userEmail, '', 10000);
								
								$peanutlabs = '';
								foreach($emailOffersEachDate as $eo){
									switch ($eo['offer_name']) {
										case 'PeanutLabs':
											if ($site['domain'] == 'lalaloot.com') {
												$args['userID'] = $testUid.'-4684-'.substr(md5($testUid.'4684'.'7a023e4a695dd2735bdd3d407b575fea'), 0, 10);
											} elseif ($site['domain'] == 'cashdazzle.com') {
												$args['userID'] = $testUid.'-4676-'.substr(md5($testUid.'4676'.'f211973200aed87ef558d3ec364765dc'), 0, 10);
											} else {
												$args['userID'] = $testUid.'-4712-'.substr(md5($testUid.'4712'.'e3d24dc5754aa85b269054f688423a52'), 0, 10);
											}
											break;
										case 'RadiumOne':
										case 'SuperRewards':
											$args['userID'] = $testUid;
											break;
										default:
											$args['userID'] = 0;
									}
									$peanutlabs .= '<div style="text-align:center; padding-bottom: 15px;">';
									$peanutlabs .= '<a href="' . str_replace('INSERT_END_USER_ID', $args['userID'], $eo['link_url']) . '" target="_blank"><img src="' . $eo['picture_url'] . '" width="' . $eo['picture_width'] . '" height="' . $eo['picture_height'] . '" alt="'.$eo['name'].'"/></a>';
									$peanutlabs .= '</div>';
								}
								
								$args['emailOffer'] = $peanutlabs;
								$args['emailId'] = $emailId;
								
								$translatedTemplate = Util::translate($template, $args);
								
								$emailModel->updateEmailContentById($emailId, $translatedTemplate);
							}
						}
					}
				}
				
				return $this->_redirect( url('offermanager') );
			}
		}
		
	}